<?php
/** 
 * @package AMFramework
 * @subpackage Data
 * @author Marcu Alexandru
 * @version 1.0 $Build 305b  
 */
defined('_INDEX') or die('Restricted');

class Amfw_Search {
	private $_tables = array();
	private $_strings = array();
	
	private $_result = null;
	
	function useTable($table) {
		$this->_tables[] = $table;
	}
	
	function setSearchStr($string) {
		if(strrpos($string,' ') !== false) {
			$string = explode(' ',$string);
			foreach($string as $str) {	// viitoare implementare dictionare cuv comune, etc
				if(strlen($str) >= 3 ) { //cel putin 3 caractere pentru a fi luat in considerare
					$this->_strings[] = strtolower($str);
				}
			}//~!foreach()
		} else {
			if(strlen($string) >= 3) {
				$this->_strings[] = strtolower($string);
			}
		}
	}
	
	function fetchResult() {
		$db =& Amfw_Factory::getDb();
		$result = array(); // rezultatul va fi array asociativ indexat dupa numele tabelului
		//folosim 1 query/tabel
		foreach ($this->_tables as $table) {
			$query = $this->_buildSearchQuery($table->get('_table'), $table->get('_pk'), $table->get('_contentf'), $table->get('_keywordsf'));
			$db->setQuery($query,true);
			$tmpRes = $db->getAssocList();
			$finalRes = $this->resultProcessing($tmpRes,$table->get('_keywordsf'),$table->get('_contentf'),$table->get('_pk'));
			$result[$table->get('_table')] = $finalRes;
		}
		
		$this->_result = $result;
	}
	
	function getResult() {
		return $this->_result;
	}
	
	function resultProcessing($tmpRes, $keywordsField, $contentFields, $id) {
		//var_dump($result);
		//var_dump($keywords);
		//die();
		$final = array();
		$mask = array();
		//generarea "mastii"
		foreach($tmpRes as $result) {	// for each row
			$keywords = $result[$keywordsField];
			$content = "";
			foreach($contentFields as $cf) {
				$content .= strtolower($result[$cf]);	//inlaturam problema case-sensitive 
			}
			$mask[$result[$id]] = 0;
			foreach($this->_strings as $string) {	//pentru fiecare string crestem contorul din masca
				$keywHits = substr_count($keywords,$string);
				$contHits  = substr_count($content,$string);
				$mask[$result[$id]] += ($contHits + ($keywHits*10));//keywords have 10 times more relevance
			}
		}
		//sortam masca
		arsort($mask,SORT_NUMERIC);	//associative reverse sort(pastreaza asocierea intre index si valoare)

		foreach($mask as $key => $value) {
			foreach($tmpRes as $result) {
				if($result[$id] == $key) {
					$result['hits']=$value;
					$final[] = $result;
					break;//get to the next mask key
				}
			}
		}

		return $final;
	}
	
	private function _buildSearchQuery($table,$pk,$contentf,$keywords) {
		$contentf[] = $keywords;//adding the keywords field to the content fields(as it doesnt make anything here)
		$query = "SELECT $pk,".implode(',',$contentf)." FROM $table WHERE ";
		foreach($contentf as $field) {
			foreach($this->_strings as $string) {
				$query .= " $field LIKE '%$string%' OR ";
			}
		}
		$query .= "0";
		
		return $query;
	}
	
	
}